package com.easybuilder.core.im.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;

public class IMServerReadHandler extends ChannelInboundHandlerAdapter {

    /** 读取数据 */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buffer = (ByteBuf) msg;
        System.out.println(ctx.channel().remoteAddress() + "：" + buffer.toString(CharsetUtil.UTF_8));
    }

    /** 读取数据完毕 */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.channel().write(Unpooled.copiedBuffer("已接收消息", CharsetUtil.UTF_8));
        ctx.channel().flush();
    }

    /** 异常处理 */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
